#include <ResourceWindow.h>
#include <QToolBar>
#include <iostream>
#include <ModeControlDialog.h>
#include <EDesignerCore.h>
#include <CommandManager.h>
#include <Commands.h>
#include <QDir>
#include <QFileDialog>
ResourceWindow::ResourceWindow( QWidget *parent /*= 0*/ ) : WindowBase(parent)
{
    setWindowTitle("Resource Window");
    getWindowShowHideAction()->setText("Resource");
    getWindowShowHideAction()->setChecked(true);
    getDockWidget()->setWindowTitle("Resource Window");

    m_treeView = new QTreeView();
    m_fileSystemModel = new QFileSystemModel();

    m_treeView->setModel(m_fileSystemModel);

    setRootPath(QDir::currentPath() + QString("/project/modelData"));
    setCentralWidget(m_treeView);

    //creat actions
    QAction* action_add_file = new QAction(QIcon("resource/action_add_file.png"), QString("Add File"),0);
    QAction* action_add_folder = new QAction(QIcon("resource/action_add_folder.png"), QString("Add Folder"),0);
    QAction* action_delete = new QAction(QIcon("resource/action_delete_node.png"), QString("Delete File/Folder"),0);

    m_actionList.push_back(action_add_file);
    //m_actionList.push_back(action_add_folder);
    m_actionList.push_back(action_delete);

    QObject::connect(action_add_file, SIGNAL(triggered()), this, SLOT(actionAddFileTriggered()));
    QObject::connect(action_add_folder, SIGNAL(triggered()), this, SLOT(actionAddFolderTriggered()));
    QObject::connect(action_delete, SIGNAL(triggered()), this, SLOT(actionDeleteTriggered()));


    //add tool bar
    QToolBar* tool_bar = new QToolBar();
    tool_bar->addActions(m_actionList);
    this->addToolBar(tool_bar);

    //fill in menu
    m_menu = new QMenu(QString("Object Inspector"));
    m_menu->addActions(m_actionList);

}

ResourceWindow::~ResourceWindow()
{

}

Qt::DockWidgetArea ResourceWindow::dockWidgetArea()
{
    return Qt::DockWidgetArea::LeftDockWidgetArea;
}



void ResourceWindow::setUpInitialShowHide()
{
    getWindowShowHideAction()->setChecked(false);

}

void ResourceWindow::setRootPath(const QString &rootPath)
{
    m_fileSystemModel->setRootPath(rootPath);
    m_treeView->setRootIndex(m_fileSystemModel->index(rootPath));
}

QString ResourceWindow::getCurrentFileAbsolutePath()
{
    QModelIndex current_index = m_treeView->currentIndex();
    QFileInfo fileInfo = m_fileSystemModel->fileInfo(current_index);
    return fileInfo.absoluteFilePath();
}

QString ResourceWindow::getCurrentFilePath()
{
    QModelIndex current_index = m_treeView->currentIndex();
    //QFileInfo fileInfo = m_fileSystemModel->fileInfo(current_index);
    QString absFilePath = m_fileSystemModel->filePath(current_index);
    QString parentPath = QDir::currentPath();
    //int startIndex = absFilePath.indexOf("/project", absFilePath.size()-1);
    QString fielPath = absFilePath.remove(0, parentPath.size());
    std::cout<<"ResourceWindow::getCurrentFilePath"<<fielPath.toStdString()<<std::endl;
    return fielPath;
}

void ResourceWindow::actionAddFileTriggered()
{
    //std::cout<<"ResourceWindow::actionAddFileTriggered"<<std::endl;
    QString file_name = QFileDialog::getOpenFileName(0, "Add Resource File","","*.*");
    QFileInfo fileInfo(file_name);
    QString newFileName = QDir::currentPath() + QString("/project/modelData/") + fileInfo.fileName();
    std::cout<<"ResourceWindow::actionAddFileTriggered "<<newFileName.toStdString()<<std::endl;
    copyFileToPath(file_name, newFileName, true);
}

void ResourceWindow::actionAddFolderTriggered()
{
    std::cout<<"ResourceWindow::actionAddFolderTriggered"<<std::endl;

}

void ResourceWindow::actionDeleteTriggered()
{
    std::cout<<"ResourceWindow::actionDeleteTriggered"<<std::endl;
    QList<QModelIndex> selected_indexes = m_treeView->selectionModel()->selectedIndexes();
    for(int i = 0; i<selected_indexes.size(); i++)
    {
        m_fileSystemModel->remove(selected_indexes.at(i));
    }
}

bool ResourceWindow::copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist)
{
    toDir.replace("\\","/");
    if (sourceDir == toDir){
        return true;
    }
    if (!QFile::exists(sourceDir)){
        return false;
    }
    QDir *createfile     = new QDir;
    bool exist = createfile->exists(toDir);
    if (exist){
        if(coverFileIfExist){
            createfile->remove(toDir);
        }
    }//end if

    if(!QFile::copy(sourceDir, toDir))
    {
        return false;
    }
    return true;
}
